﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bronze.XXTong.DAL.DatabaseSpecific;
using Bronze.XXTong.DAL.Linq;
using Bronze.XXTong.DAL.HelperClasses;
using Bronze.XXTong.DAL.EntityClasses;

namespace Bronze.XXT.BLL
{
    public class CallBilling
    {
        /// <summary>
        /// 保存通话清单
        /// </summary>
        /// <param name="strIcCardNum">刷卡卡号</param>
        /// <param name="strPhoneNum">使用的共话号码</param>
        /// <param name="callInfos">0 开始时间 1 通话时长 2 被叫号码</param>
        /// <returns>0 卡号无效，1 卡号有效</returns>
        public static string ReciveCallBilling(string strIcCardNum, string strPhoneNum, string[,] callInfos)
        {
            string strResult = "0";
            Guid userID = Guid.Empty;
            CallLogEntity entity = null;
            EntityCollection ec = new EntityCollection();
            int intUserType = 0;
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                LinqMetaData metaData = new LinqMetaData(adapter);

                userID = metaData.Student.Where(o => o.FIcCardNumber == strIcCardNum).Select(o => o.FStuID).SingleOrDefault();
                intUserType = 2;
                if (userID == Guid.Empty)
                {
                    userID = metaData.UserInfo.Where(o => o.FIcCardNumber == strIcCardNum).Select(o => o.FUserID).SingleOrDefault();
                    intUserType = 1;
                }
                if (userID != Guid.Empty && callInfos.Length > 0)
                {
                    for (int i = 0; i < callInfos.GetLength(0); i++)
                    {
                        entity = new CallLogEntity();
                        entity.FId = Guid.NewGuid();
                        entity.FCallerId = userID;
                        entity.FPhoneSerialNumber = strPhoneNum;
                        entity.FTargetNumber = callInfos[i, 2];
                        entity.FCallTime = CommonMethod.ConvertStringToDateTime(callInfos[i, 0]);
                        entity.FCallTimeLength = Convert.ToInt32(callInfos[i, 1]);
                        entity.FType = intUserType;
                        ec.Add(entity);
                    }
                    adapter.SaveEntityCollection(ec);
                }
            }
            return strResult;
        }
    }
}
